

#include "HeaderFiles.h"


STR_USER_GPIO userGPIO[GPIO_NUM_ALL];



/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/

void user_gpio_init(void)
{
	userGPIO[IN_ELE_POS_X1].num_group = GPIOA;
	userGPIO[IN_ELE_POS_X1].num_pin = GPIO_PIN_11;
	userGPIO[IN_ELE_POS_X1].direction = GPIO_INPUT;
	
	userGPIO[IN_ELE_POS_X2].num_group = GPIOA;
	userGPIO[IN_ELE_POS_X2].num_pin = GPIO_PIN_12;
	userGPIO[IN_ELE_POS_X2].direction = GPIO_INPUT;
	
	userGPIO[IN_CHARGE_STATUS].num_group = GPIOC;
	userGPIO[IN_CHARGE_STATUS].num_pin = GPIO_PIN_7;
	userGPIO[IN_CHARGE_STATUS].direction = GPIO_INPUT;
	

	
	
	userGPIO[OUT_LED1_BOARD].num_group = GPIOB;
	userGPIO[OUT_LED1_BOARD].num_pin = GPIO_PIN_4;
	userGPIO[OUT_LED1_BOARD].direction = GPIO_OUTPUT;
	
	userGPIO[OUT_LED2_BOARD].num_group = GPIOB;
	userGPIO[OUT_LED2_BOARD].num_pin = GPIO_PIN_5;
	userGPIO[OUT_LED2_BOARD].direction = GPIO_OUTPUT;

	userGPIO[OUT_LED3_EXTERN].num_group = GPIOC;
	userGPIO[OUT_LED3_EXTERN].num_pin = GPIO_PIN_12;
	userGPIO[OUT_LED3_EXTERN].direction = GPIO_OUTPUT;

	userGPIO[OUT_BUZZER].num_group = GPIOC;
	userGPIO[OUT_BUZZER].num_pin = GPIO_PIN_6;
	userGPIO[OUT_BUZZER].direction = GPIO_OUTPUT;

	userGPIO[OUT_RELAY_CHARGE].num_group = GPIOD;
	userGPIO[OUT_RELAY_CHARGE].num_pin = GPIO_PIN_2;
	userGPIO[OUT_RELAY_CHARGE].direction = GPIO_OUTPUT;
	
	userGPIO[OUT_RELAY_HOLD].num_group = GPIOB;
	userGPIO[OUT_RELAY_HOLD].num_pin = GPIO_PIN_8;
	userGPIO[OUT_RELAY_HOLD].direction = GPIO_OUTPUT;
	
	userGPIO[OUT_WASTE_PUMP].num_group = GPIOB;
	userGPIO[OUT_WASTE_PUMP].num_pin = GPIO_PIN_9;
	userGPIO[OUT_WASTE_PUMP].direction = GPIO_OUTPUT;
	
	userGPIO[OUT_ACTUATE_X1].num_group = GPIOA;
	userGPIO[OUT_ACTUATE_X1].num_pin = GPIO_PIN_14;
	userGPIO[OUT_ACTUATE_X1].direction = GPIO_OUTPUT;
	
	userGPIO[OUT_ACTUATE_X2].num_group = GPIOA;
	userGPIO[OUT_ACTUATE_X2].num_pin = GPIO_PIN_15;
	userGPIO[OUT_ACTUATE_X2].direction = GPIO_OUTPUT;
	
}



/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/

void set_gpio_output(STR_USER_GPIO *gpioHdl, FlagStatus level)
{
	
	uint32_t gpio_group = gpioHdl->num_group;
	uint32_t gpio_num   = gpioHdl->num_pin;
	
	if(level == SET)
	{
		gpio_bit_set(gpio_group, gpio_num);
	}
	else
	{
		gpio_bit_reset(gpio_group, gpio_num);
	}
	
}

/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/

FlagStatus get_gpio_input(STR_USER_GPIO *gpioHdl)
{
	uint32_t gpio_group = gpioHdl->num_group;
	uint32_t gpio_num   = gpioHdl->num_pin;
	
	return gpio_input_bit_get(gpio_group, gpio_num);
}








/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/

void heartbeat_led_init(uint16_t version_num)
{
	rcu_periph_clock_enable(RCU_GPIOB);
	rcu_periph_clock_enable(RCU_AF);

	gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_5);
	gpio_bit_reset(GPIOB, GPIO_PIN_5); 
	
	userHdl.param.led_num = GPIO_PIN_5;
	userHdl.param.led_periph = GPIOB;
		
	
}


/*!
    \brief      system digital signal IO init
    \param[in]  none
    \param[out] none
    \retval     none
*/

void general_gpio_init(void)
{
	// enable gpio clock
	rcu_periph_clock_enable(RCU_GPIOA);	
	rcu_periph_clock_enable(RCU_GPIOB);		
	rcu_periph_clock_enable(RCU_GPIOD);
    rcu_periph_clock_enable(RCU_GPIOE);
	rcu_periph_clock_enable(RCU_GPIOF);
	rcu_periph_clock_enable(RCU_GPIOG);
	
    rcu_periph_clock_enable(RCU_AF);
	
	user_gpio_init();
	
	gpio_pin_remap_config(GPIO_SWJ_SWDPENABLE_REMAP, ENABLE);
	
	for(uint8_t i=0; i<GPIO_NUM_ALL; i++)
	{
		uint32_t group = userGPIO[i].num_group;
		uint32_t pin   = userGPIO[i].num_pin;
		
		if(userGPIO[i].direction == GPIO_INPUT)
		{
			gpio_init(group, GPIO_MODE_IPU, GPIO_OSPEED_50MHZ, pin);
		}
		else
		{
			gpio_init(group, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, pin);
			gpio_bit_reset(group, GPIO_PIN_2);   
			
		}
	}
	
}




/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/

#if 0
void EXTI1_IRQHandler(void)
{
	
	wheel_speed_pulse_cnt[1]++;
	
	
	if(userHdl.ctrl.speedRef_rps[WHEEL_RIGHT] >= 0)
	{
		wheel_encoder_cnt[1]--;	
	}
	else
	{
		wheel_encoder_cnt[1]++;	
	}
	
	exti_interrupt_flag_clear(EXTI_1); 
}

/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/
 
void EXTI10_15_IRQHandler(void)
{
	
	wheel_speed_pulse_cnt[0]++;
	
	if(userHdl.ctrl.speedRef_rps[WHEEL_LIFT] >= 0)
	{
		wheel_encoder_cnt[0]++;	
	}
	else
	{
		wheel_encoder_cnt[0]--;	
	}
	
	exti_interrupt_flag_clear(EXTI_13); 
	
}
#endif


